package com.phkj.qdh.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.repository.LoginRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest

class LoginVM(val login: LoginRepository) : BaseVM() {

    private val _userBean = MutableSharedFlow<String>()
    val userBean get() = _userBean

    fun login(map: HashMap<String, Any?>) {
        startLoading()
        requestIO {
            login.login(map).catch { cause ->
                loadFail()
            }.collectLatest {
               loadSuccess()
                _userBean.emit(it)
            }
        }
    }


}

fun getLoginVMProvider() = LoginVMProvider(LoginRepository)

class LoginVMProvider(private val login: LoginRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return LoginVM(login) as T
    }
}
